//
//  XLPushTool.m
//  industry
//
//  Created by xl on 2020/12/14.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLPushTool.h"
#import <UserNotifications/UserNotifications.h>

@interface XLPushTool ()<UNUserNotificationCenterDelegate>

@end
@implementation XLPushTool
XLShareInstance_implementation(XLPushTool)

- (void)configXLPush {
    // 注册推送
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {// 10.0+
        UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
        center.delegate = self;
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
                [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert|UNAuthorizationOptionSound|UNAuthorizationOptionBadge)
                                      completionHandler:^(BOOL granted, NSError * _Nullable error) {
                    // Enable or disable features based on authorization.
                    if (granted) {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            [[UIApplication sharedApplication] registerForRemoteNotifications];
                        });
                    }
                }];
        }];
    } else {// 8.0，9.0
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge                                                                                         |UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    }
}

- (void)bindXLIdentifier {
    if (!ISEmptyNumber(XL_UserInfoData.userId)) {
        [[PushService sharedService] pushBindWithUserId:[NSString stringWithFormat:@"user%@", XL_UserInfoData.userId] completion:^(NSException *error) {
            XLLog(@"绑定error=%@", error);
        }];
    }
}

- (void)unbindXLIdentifier {
    if (!ISEmptyNumber(XL_UserInfoData.userId)) {
        [[PushService sharedService] pushUnBindWithUserId:[NSString stringWithFormat:@"user%@", XL_UserInfoData.userId] completion:^(NSException *error) {
            XLLog(@"解除errror = %@", error);
        }];
    }
}


#pragma mark - UNUserNotificationCenterDelegate
//iOS10新增：处理前台收到通知的代理方法
//-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
//    NSDictionary * userInfo = notification.request.content.userInfo;
//    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//        [UMessage setAutoAlert:NO];
//        [self handleNoticeMessageWithContent:[userInfo objectForKey:@"custom"]];
//        //必须加这句代码
//        [UMessage didReceiveRemoteNotification:userInfo];
//    }else{
//        //应用处于前台时的本地推送接受
//    }
//    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
//}

//iOS10新增：处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [self handleNoticeMessageWithContent:[userInfo objectForKey:@"custom"]];
        //必须加这句代
        
    }else{
        //应用处于后台时的本地推送接受
    }
}

#pragma mark - setter/getter
//处理通知消息
- (void)handleNoticeMessageWithContent:(NSString *)content {

}
@end
